<?php
namespace app\common\service\SmsService;

use app\Normal;
use app\common\service\SmsService\Config\Template;
use app\common\service\RequestService\Http;

class Sms
{
    /**
     * instance
     * @access private
     * @var object
     */
    private static $instance;
    
    /**
     * 短信通道
     * @access private
     * @var    array
     */
    private $way;
    
    /**
    * @construct
    */
    private function __construct($way){
        $this->way = $way;
    }
    
    /**
     * @clone
     */
    private function __clone(){}
    
    /**
     * @get instance
     * @return   static
     */
    public static function instance($way = [])
    {
        if (empty(self::$instance)) {
            self::$instance = new static($way);
        }
        
        return self::$instance;
    }
    
    /**
    * @发送短信
    * @param    array $param
    * @access   public
    * @return   array
    */
    public function sendMessage(array $param = []):array
    {
        if (empty($this->way['code']) || !method_exists($this, $this->way['code'])) {
            return Normal::returnCode(Normal::FAIL,"短信通道不存在");
        }
        
        if (empty($param['mobile'])) {
            return Normal::returnCode(Normal::FAIL,"发送手机号为空");
        }
        
        if (empty($param['temp'])) {
            return Normal::returnCode(Normal::FAIL,"短信模板为空");
        }
        
        if (!in_array($param['temp'], array_keys(Template::$temp))) {
            return Normal::returnCode(Normal::FAIL,"短信模板错误");
        }
        
        $temp = !empty($param['temp']) && in_array($param['temp'], array_keys(Template::$temp))
                ? Template::$temp[$param['temp']] : Template::$temp['default'];
//        foreach ($param as $key => $val) {
//            if (!in_array($key, ['mobile', 'temp'])) {
//                $temp = str_replace('{'.$key.'}', $val, $temp);
//            }
//        }
        $temp = str_replace('{code}', $param['code'], $temp);
        $message_data = [
            'mobile' => $param['mobile'],
            'temp'   => $temp,
        ];
        return $this->{$this->way['code']}($message_data);
        
    }
    
    /**
    * @默认短信
    * @param    array $param
    * @access   protected
    * @return   array
    */
    protected function sms_default($param = []):array
    {
        $config = $this->way['value'];
        $message = iconv("UTF-8", "gb2312", $param['temp'] . $config['signature']);
        
        $content = Http::get("https://sdk2.028lk.com/sdk2/BatchSend.aspx?CorpID=" . $config['uid'] . "&Pwd=" . $config['passwd'] . "&Mobile=" . $param['mobile'] . "&Content=" . $message . "&SendTime=&cell=");

        if ($content >= 0) {
            return Normal::returnCode(Normal::SUC,"发送成功");
        }
        return Normal::returnCode(Normal::FAIL,"发送失败");
    }
    
    /**
    * @微网通联短信
    * @param    array
    * @access   protected
    * @return   array
    */
    protected function sms_wwtl($param = []):array
    {
        $config = $this->way['value'];
        $message = $param['temp'].$config['signature'];
        
        $url = "http://cf.51welink.com/submitdata/Service.asmx/g_Submit";
        
        $data= [
            'sname'   => $config['sname'],
            'spwd'    => $config['spwd'],
            'scorpid' => '',
            'sprdid'  => $config['sprdid'],
            'sdst'    => $param['mobile'],
            'smsg'    => $message,
        ];
        $res = Http::post($url, $data);
        $res = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if ($res['State'] == 0) {
            return Normal::returnCode(Normal::SUC,"发送成功");
        }
        return Normal::returnCode(Normal::FAIL,"发送失败");
    }
}